<?php
namespace jy_wxshifu;
defined('BY_JYA') or exit('error');
/*
*/
class member extends \plugin{
	
	/*
		会员注册
	*/
	public function register($member=array()){
		$update=$member;
		if(!$member['uniacid']){
			$member['uniacid']=$_SESSION['uniacid'];
		}
		if(!$member['mid']){
			$member['mid']=$_SESSION['uid'];
		}
		
		if(!$member['createtime']){
			$member['createtime']=TIMESTAMP;
		}
		if(!$member['mid']){
			return error('师傅会员注册失败，缺少会员ID.');
		}
		$ispid=$this->get('member',array('mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']),array('id'));
		if($ispid){
			$this->update('member',$update,array('mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']));
			return $ispid['id'];
		}
		$this->insert('member',$member);
		return $this->insertid();
	}
	/*
		申请成为师傅
	*/
	public function apply_agent($pid,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$apply=$this->get('member',array('id'=>$pid,'uniacid'=>$_SESSION['uniacid']));
		if($apply['status']==1){
			exi('该服务点你已申请过了！','error');
		}
		if(!$this->setting['become_check']){
			//师傅无需审核，此处自动审核
			$this->check_apply($apply_id,1);
		}
		return true;
	}
	/*
		审核师傅
	*/
	public function check_apply($apply_id,$status,$remark=''){
		if(!$apply_id || !$status){
			return;
		}
		$member=$this->get('member',array('id'=>$apply_id));
		if(!$member){
			return;
		}
		if($status==1){
			//审核通过，修改会员状态，修改上线相关数据
			$member_update=array(
				'status'=>1,
			);
			$this->update('member',$member_update,array('id'=>$member['id'],'uniacid'=>$_SESSION['uniacid']));
			$this->model('notice')->notice_check_apply($member);
		}else{
			$member_update=array(
				'status'=>$status,
			);
			$this->update('member',$member_update,array('id'=>$member['id'],'uniacid'=>$_SESSION['uniacid']));
			$update['remark']=$remark;
		}
		return true;
	}
	/*
		禁用师傅
	*/
	public function stop_agent($agent_id){
		$this->update('member',array('stop'=>1),array('mid'=>$agent_id));
	}
	/*
		恢复师傅
	*/
	public function start_agent($agent_id){
		$this->update('member',array('stop'=>0),array('mid'=>$agent_id));
	}
	public function getrank($star=0,$ordernum=0){
		$rank=$star*100+$ordernum/10;
		if($star>4.5){
			$rank +=30;
		}
		return $rank;
	}
	public function setcomment($postdata=array()){
		$data=array(
			'content'=>$postdata['content'],
			'images'=>ijson_encode($postdata['images']),
			'star'=>intval($postdata['star']),
		);
		$comment_id=$this->pdo_getcolumn('comments',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>intval($postdata['orderid'])),'id');
		if($comment_id){
			if($postdata['show']==1){
				exi('该评价已生效显示，不可修改！','error','goback');
			}
			$this->pdo_update('comments',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$comment_id));
		}else{
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'orderid'=>intval($postdata['orderid']),
				'createtime'=>TIMESTAMP,
			);
			$data=array_merge($insert,$data);
			$data['shifuid']=$postdata['shifuid'];
			$this->pdo_insert('comments',$data);
		}
		return true;
	}
}